package com.semlab.client.fwk.ap;

import java.util.logging.Logger;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.semlab.client.fwk.GoToPlaceRequestEvent;

public abstract class DefaultPlaceController extends PlaceController {
	private static final Logger log = Logger.getLogger(DefaultPlaceController.class.getName());


	@Inject
	public DefaultPlaceController(EventBus eventBus) {
		super(eventBus);
		eventBus.addHandler(GoToPlaceRequestEvent.TYPE, new GoToPlaceRequestEvent.Handler<GoToPlaceRequestEvent>() {
			@Override
			public void on(GoToPlaceRequestEvent e) {
				log.fine("$$ Place change :"+e.getPlaceToGo());
				goTo(e.getPlaceToGo());
			}
		});
	}

	@Override
	public Place getWhere() {
		return Place.NOWHERE;
	}
	
	@Override
	public void goTo(Place newPlace) {
		if(isPlaceChangeAllowed(newPlace)){
			super.goTo(newPlace);
		}
	}
	
	/**
	 * Custom place change handling
	 */
	protected abstract boolean isPlaceChangeAllowed(Place newPlace);
	
}
